<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>标签打印</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <{webcontrol type='LoadJsCss' src="Resource/Script/layui/layui/css/layui.css"}>
    <{webcontrol type='LoadJsCss' src="Resource/Script/layui/style/admin.css"}>
    <script src="https://sev7.eqinfo.com.cn:8443/CLodopfuncs.js"></script>
    <{webcontrol type='LoadJsCss' src="Resource/Script/jquery.js"}>
    <style type="text/css">
        .layui-input{display: inline-block;width: 50px;height: 30px;}
        .ssbprinter-select{min-width: 160px;display: none;max-width: 60%;width: auto;font-size: 12px;}
        .ssbprinter-select option{font-size: 12px;}
    </style>
    <script type="text/javascript">
        var object = <{$row|@json_encode}>;
        var sys = <{$sys|@json_encode}>;
        function printCode(key) {
            alert(key);
        }
    </script>
</head>
<body>
    <div class="layui-fluid" id="LAY-component-grid-all">
        打印标签：点击打印直接打印标签<br/>
        <span id="printName">无可用打印机</span>
        <select name="city" class="layui-input layui-unselect ssbprinter-select">
            <option value="-1">使用默认打印机</option>
        </select>
        <hr class="layui-bg-green">
        <div class="layui-row layui-col-space10">
            <{foreach from=$row.Products item=item key=key}>
            <div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
                <!-- 填充内容 -->
                <div class="layui-card">
                    <div class="layui-card-header">条码：<{$item.code}></div>
                    <div class="layui-card-body">
                        <div class="layui-row">
                            <div class="layui-col-sm12 layui-col-md12 layui-col-lg12">
                                产品编号：<{$item.Product.proCode}>
                            </div>
                            <div class="layui-col-sm12 layui-col-md12 layui-col-lg12">
                                产品名称：<{$item.Product.proName}>
                            </div>
                            <div class="layui-col-sm12 layui-col-md12 layui-col-lg12">
                                花型颜色：<{$item.proDesc}>
                            </div>
                            <div class="layui-col-sm12 layui-col-md12 layui-col-lg12">
                                本次数量：<{$item.cnt}>
                            </div>
                            <div class="layui-col-sm12 layui-col-md12 layui-col-lg12">
                                样品类型：<{$item.type}>
                            </div>
                            <div class="layui-col-sm12 layui-col-md12 layui-col-lg12">
                                样品位置：<{$item.kuweiName}>
                            </div>
                            <div class="layui-col-sm12 layui-col-md12 layui-col-lg12" style="text-align: right;">
                                <{if $item.Print.error}>
                                <button class="layui-btn layui-btn-sm layui-btn-disabled"><{$item.Print.error}></button>
                                <{else}>
                                <input type="text" id="input_<{$key}>" title="打印份数" placeholder="打印份数" autocomplete="off" value="1" class="layui-input layui-input-sm">份
                                <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="<{$item.Print.templete}>('<{$key}>')">打印</button>
                                <{/if}>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <{/foreach}>
        </div>
    </div>


    <!-- 二维码打印 -->
    <!-- 二维码的打印模版 -->
<script language="javascript">
    var LODOP = CLODOP;
    var printer = LODOP.Printers;
    LODOP.SET_LICENSES("常州易奇信息科技有限公司","664717080837475919278901905623","","");
    _printId = "<{$print_id_clodop}>";
    // console.log('printer',printer);
    var printerCur = '';
    var printerCurId = '';
    var printerName = '';
    var ssbprinter = [];
    var ssbprinterDefault = -1;
    for(var k in printer.list){
        var print = printer.list[k];
        if(print.subdevlist.length > 0){
            for (var i = 0; i < print.subdevlist.length; i++) {
                var _p = print.subdevlist[i];
                if(_p.id == _printId){
                    printerCur   = print.name+','+_p.id+';'+_p.name;
                    printerName  = _p.name
                    printerCurId = _p.id;
                    if(_p.ssbprinter){
                        ssbprinter = _p.ssbprinter.list || [];
                        ssbprinterDefault = _p.ssbprinter.default;
                    }
                    break;
                }
            }
        }
    }

    //设置连接的打印机所在的电脑
    LODOP.SET_PRINTER_INDEXA(printerCur);
    if(ssbprinter.length > 0){
        for(var i=0 ;i<ssbprinter.length ;i++){
            var _sel = '';
            if(ssbprinterDefault == i){
                _sel = "selected";
            }
            $('.ssbprinter-select').append(`<option ${_sel} value='${i}'>${ssbprinter[i].name}</option>`);
        }
        $('.ssbprinter-select').css({display:'inline-block'});
    }
    if(printerName){
        $('#printName').text(printerName);
    }
    // console.log('printer',printerCur);


    var _LODOP = null;
    //处理模版配置的信息，尺寸，字体大小等
    function configFormat(_config){
        //左边栏宽度
        var widthLen = _config.width || 13;
        //纸张宽度
        var pageWidth = _config.pageWidth;
        //纸张高度
        var pageHeight = _config.pageHeight;
        //计算左边标题文字需要的宽度
        var widthLeft = parseFloat(widthLen - 0.5);

        //样品值显示在右侧的起始位置
        var marginLleft = parseFloat(widthLen);

        //开始打印的上边距
        var topMargin = 2;
        //开始打印的左边距
        var leftMargin = 2;

        //内容表格的实际使用宽度:纸张宽度去掉左右边距
        var tableWidth = pageWidth - leftMargin - leftMargin;
        //打印的实际高度:去掉上下边距
        var tableHeight = pageHeight - topMargin - topMargin;

        //右边文字的占用宽度
        var widthRight = parseFloat(tableWidth-widthLeft-1);

        //计算显示主要内容的高度是多少,去掉公司名称的高度
        var rowHeight = tableHeight / (1 + _config.fileds.length);

        var fontSizeSys = _config.fontSize || 0;
        var fontSize = fontSizeSys || ((widthRight > 70 && rowHeight > 6) ? 11 : 9);
        var rowTop = rowHeight / 3.5;
        var inputHeight = rowHeight - rowTop;
        return {pageWidth,pageHeight,topMargin,rowTop,leftMargin,tableWidth,inputHeight,fontSize,rowHeight,widthLeft,marginLleft,widthRight,tableHeight};
    }

    //打印入口
    function qrcode(key) {
        _LODOP = LODOP;
        //获取到配置
        var obj = object.Products[key];
        var config = configFormat(obj.Print);
        // console.log(config);
        CreateOneFormPageQrcode(obj ,config);
        if($('#input_'+key)){
            var count = $('#input_'+key).val() || 1;
            _LODOP.SET_PRINT_COPIES(count);//指定份数
        }
        // console.log('print',$('.ssbprinter-select').val());

        if($('.ssbprinter-select').val() != '-1'){
            _LODOP.SET_PRINTER_INDEXA($('.ssbprinter-select').val());
        }

        if(printerCurId == 'C2215948706'){
            _LODOP.PREVIEW();
        }else{
            _LODOP.PRINT();
            // _LODOP.PREVIEW();
        }
        // _LODOP.PREVIEW();
        // _LODOP.PRINT();
        // _LODOP.PRINT_DESIGN();
    };

    function CreateOneFormPageQrcode(obj,config){
        // _LODOP.SET_PRINTER_INDEXA(printerCur);
        _LODOP.SET_PRINT_PAGESIZE(1,config.pageWidth+"mm",config.pageHeight+"mm",'条码标签');
        //大矩形
        // _LODOP.ADD_PRINT_RECT(topMargin+"mm",leftMargin+"mm",tableWidth+"mm",tableHeight+"mm",0,1);

        if(sys.logo){
            _LODOP.ADD_PRINT_IMAGE((config.topMargin+config.rowTop)+"mm",(config.tableWidth-sys.logo.ws-1)+"mm",config.tableWidth+"mm",sys.logo.hs+"mm","<img border='0' src='"+sys.logo.path+"'/>");
            _LODOP.SET_PRINT_STYLEA(0,"Stretch",2);//按原图比例(不变形)缩放模式
        }else{
            _LODOP.ADD_PRINT_TEXT((config.topMargin+config.rowTop)+"mm",config.leftMargin+"mm",config.tableWidth+"mm",config.inputHeight+"mm",sys.compName);

            _LODOP.SET_PRINT_STYLEA(0,"Alignment",2);
            _LODOP.SET_PRINT_STYLEA(0,"FontSize",config.fontSize);
        }


        for(var i=0;i<obj.Print.fileds.length;i++){
            var len = config.topMargin + config.rowTop + parseFloat(config.rowHeight*(i+1));
            var temp = obj.Print.fileds[i] ? obj.Print.fileds[i] : '';

            _LODOP.ADD_PRINT_TEXT(len+"mm",config.leftMargin+"mm",config.tableWidth+"mm",config.inputHeight+"mm",temp.title ? temp.title+': ' +temp.value : '');
            _LODOP.SET_PRINT_STYLEA(0,"Alignment",1);
            _LODOP.SET_PRINT_STYLEA(0,"FontSize",config.fontSize);
            // _LODOP.SET_PRINT_STYLEA(0,"FontSize",10);
            // _LODOP.SET_PRINT_STYLEA(0,"Bold",1);

            // _LODOP.ADD_PRINT_TEXT(len+"mm",(config.marginLleft+config.leftMargin)+"mm",config.widthRight+"mm",config.inputHeight+"mm",temp.value ? temp.value : '');
            // _LODOP.SET_PRINT_STYLEA(0,"Alignment",1);
            // _LODOP.SET_PRINT_STYLEA(0,"FontSize",config.fontSize);
        }

        // _LODOP.ADD_PRINT_BARCODE("40.48mm","6.61mm","66.41mm","7.94mm","Code39",obj.proCode);
        // console.log(pageHeight-topMargin-18);
        // console.log(pageWidth-leftMargin-18);
        _LODOP.ADD_PRINT_BARCODE((config.pageHeight-config.topMargin-19)+"mm",(config.pageWidth-config.leftMargin-19)+"mm","21mm","21mm","QRCode",obj.code);
        // if(isCode == false){
        //  _LODOP.SET_PRINT_STYLEA(0,"ShowBarText",0);
        // }

    };
</script>

<!-- 条码打印 -->

<script language="javascript">
    //处理模版配置的信息，尺寸，字体大小等
    function configFormat2(obj){
        var _config = obj.Print;
        //左边栏宽度
        var widthLen = _config.width || 13;
        //纸张宽度
        var pageWidth = _config.pageWidth;
        //纸张高度
        var pageHeight = _config.pageHeight;
        //计算左边标题文字需要的宽度
        var widthLeft = parseFloat(widthLen - 0.5);

        //样品值显示在右侧的起始位置
        var marginLleft = parseFloat(widthLen);

        //开始打印的上边距
        var topMargin = 2;
        //开始打印的左边距
        var leftMargin = 2;

        //内容表格的实际使用宽度:纸张宽度去掉左右边距
        var tableWidth = pageWidth - leftMargin - leftMargin;
        //打印的实际高度:去掉上下边距
        var tableHeight = pageHeight - topMargin - topMargin;

        //右边文字的占用宽度
        var widthRight = parseFloat(tableWidth-widthLeft-1);

        var tempH = tableHeight / (2 + _config.fileds.length);
        var barCodeHeight = (tempH > 12) ? tempH : 12;

        // 字符串太短的情况
        var barCodeWidth = (tableWidth >= 68) ? 66 : (tableWidth - 2);

        var _tmpWidth = obj.code.length * 10;
        // console.log('obj.code:',obj.code);
        if(obj.code.length <= 5 && barCodeWidth > _tmpWidth) barCodeWidth = _tmpWidth;

        //计算显示主要内容的高度是多少,去掉公司名称的高度
        var rowHeight = (tableHeight - barCodeHeight) / (1 + _config.fileds.length);

        var fontSizeSys = _config.fontSize || 0;
        var fontSize = fontSizeSys || (rowHeight > 8 ? 11 : 9);

        var rowTop = rowHeight / 3.9;
        var inputHeight = rowHeight - rowTop;

        return {pageWidth,pageHeight,topMargin,rowTop,leftMargin,tableWidth,inputHeight,fontSize,rowHeight,widthLeft,marginLleft,widthRight,barCodeHeight,barCodeWidth,tableHeight};
    }

    //打印入口
    function barcode(key) {
        _LODOP = LODOP;
        //获取到配置
        var obj = object.Products[key];
        var config = configFormat2(obj);
        // console.log(config);
        CreateOneFormPageBarcode(obj ,config);

        if($('#input_'+key)){
            var count = $('#input_'+key).val() || 1;
            _LODOP.SET_PRINT_COPIES(count);//指定份数
        }

        if($('.ssbprinter-select').val() != '-1'){
            _LODOP.SET_PRINTER_INDEXA($('.ssbprinter-select').val());
        }

        if(printerCurId == 'C2215948706'){
            _LODOP.PREVIEW();
        }else{
            _LODOP.PRINT();
        }
        // _LODOP.PRINT();
        // _LODOP.PREVIEW();
        // _LODOP.PRINT_DESIGN();
    };
    function CreateOneFormPageBarcode(obj ,config){
        // _LODOP.SET_PRINTER_INDEXA(printerCur);
        // _LODOP.PRINT_INITA(topMargin+"mm",leftMargin+"mm",pageWidth+"mm",pageHeight+"mm","");
        _LODOP.SET_PRINT_PAGESIZE(1,config.pageWidth+"mm",config.pageHeight+"mm",'二维码标签');
        //大矩形
        _LODOP.ADD_PRINT_RECT(config.topMargin+"mm",config.leftMargin+"mm",config.tableWidth+"mm",config.tableHeight+"mm",0,1);

        //横线
        var lineHeight = config.topMargin + config.rowHeight;
        _LODOP.ADD_PRINT_LINE(lineHeight+"mm",config.leftMargin+"mm",lineHeight+"mm",(config.tableWidth+config.leftMargin)+"mm",0,1);
        _LODOP.ADD_PRINT_TEXT((config.topMargin+config.rowTop)+"mm",config.leftMargin+"mm",config.tableWidth+"mm",config.inputHeight+"mm",sys.compName);
        _LODOP.SET_PRINT_STYLEA(0,"Alignment",2);
        _LODOP.SET_PRINT_STYLEA(0,"FontSize",config.fontSize);
        // 竖线
        _LODOP.ADD_PRINT_LINE((config.topMargin+config.rowHeight)+"mm",(config.leftMargin+config.widthLeft-1)+"mm",(config.topMargin+config.rowHeight*(obj.Print.fileds.length+1))+"mm",(config.leftMargin+config.widthLeft-1)+"mm",0,1);

        for(var i=0;i<obj.Print.fileds.length;i++){
            var len = config.topMargin + config.rowTop + parseFloat(config.rowHeight*(i+1));
            lineHeight = lineHeight + config.rowHeight;
            _LODOP.ADD_PRINT_LINE(lineHeight+"mm",config.leftMargin+"mm",lineHeight+"mm",(config.tableWidth+config.leftMargin)+"mm",0,1);

            var temp = obj.Print.fileds[i]?obj.Print.fileds[i]:'';
            _LODOP.ADD_PRINT_TEXT(len+"mm",(config.leftMargin+1)+"mm",config.widthLeft+"mm",config.inputHeight+"mm",temp.title ? temp.title : '');
            _LODOP.SET_PRINT_STYLEA(0,"Alignment",1);
            _LODOP.SET_PRINT_STYLEA(0,"FontSize",config.fontSize);
            _LODOP.ADD_PRINT_TEXT(len+"mm",(config.marginLleft+config.leftMargin)+"mm",config.widthRight+"mm",config.inputHeight+"mm",temp.value ? temp.value : '');
            _LODOP.SET_PRINT_STYLEA(0,"Alignment",1);
            _LODOP.SET_PRINT_STYLEA(0,"FontSize",config.fontSize);
            // _LODOP.SET_PRINT_STYLE("FontSize",12);
        }

        var barCodeMargin = (config.tableWidth - config.barCodeWidth) / 2;
        //改条码类型支持大小写，Code39不支持小写
        _LODOP.ADD_PRINT_BARCODE((lineHeight+0.6)+"mm",(config.leftMargin+barCodeMargin+1)+"mm",config.barCodeWidth+"mm",(config.barCodeHeight-0.8)+"mm","128Auto",obj.code);

        // _LODOP.SET_PRINT_STYLEA(0,"ShowBarText",0);
        _LODOP.SET_PRINT_STYLEA(0,"FontSize",7);
    };


</script>


</body>
</html>